cmake_minimum_required( VERSION 3.5 )
message(STATUS "Setting up Eosio Tester @VERSION_FULL@ at @EOS_ROOT_DIR@")

SET(CMAKE_INSTALL_RPATH "${HOME}/lib;${HOME}/lib64")

set(CMAKE_CXX_COMPILER @CMAKE_CXX_COMPILER@)
set(CMAKE_C_COMPILER   @CMAKE_C_COMPILER@)

set(EOSIO_VERSION "@VERSION_FULL@")

enable_testing()

if (LLVM_DIR STREQUAL "" OR NOT LLVM_DIR)
   set(LLVM_DIR @LLVM_DIR@)
endif()

if(NOT "@LLVM_FOUND@" STREQUAL "")
   find_package(LLVM @LLVM_VERSION@ EXACT REQUIRED CONFIG)
   llvm_map_components_to_libnames(LLVM_LIBS support core passes mcjit native DebugInfoDWARF orcjit)
   link_directories(${LLVM_LIBRARY_DIR})
endif()

set( CMAKE_CXX_STANDARD 20 )
set( CMAKE_CXX_EXTENSIONS ON )
set( CXX_STANDARD_REQUIRED ON )

#adds -pthread. Ubuntu eosio.contracts build breaks without this flag specified
set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
set(THREADS_PREFER_PTHREAD_FLAG TRUE)
find_package(Threads)

if ( APPLE )
   set( CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS} ${CMAKE_CXX_FLAGS} -Wall -Wno-deprecated-declarations" )
else ( APPLE )
   set( CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS} ${CMAKE_CXX_FLAGS} -Wall")
endif ( APPLE )

set( Boost_USE_MULTITHREADED      ON )
set( Boost_USE_STATIC_LIBS ON CACHE STRING "ON or OFF" )
set( BOOST_EXCLUDE_LIBRARIES "mysql" )

add_subdirectory( @CMAKE_INSTALL_FULL_DATAROOTDIR@/leap_boost ${PROJECT_BINARY_DIR}/libraries/boost EXCLUDE_FROM_ALL)

find_library(libtester eosio_testing @CMAKE_INSTALL_FULL_LIBDIR@ NO_DEFAULT_PATH)
find_library(libchain eosio_chain @CMAKE_INSTALL_FULL_LIBDIR@ NO_DEFAULT_PATH)
find_library(libfc fc @CMAKE_INSTALL_FULL_LIBDIR@ NO_DEFAULT_PATH)
find_library(libsecp256k1 secp256k1 @CMAKE_INSTALL_FULL_LIBDIR@ NO_DEFAULT_PATH)
find_library(libbn256 bn256 @CMAKE_INSTALL_FULL_LIBDIR@ NO_DEFAULT_PATH)
find_library(libbls12-381 bls12-381 @CMAKE_INSTALL_FULL_LIBDIR@ NO_DEFAULT_PATH)

find_library(libwasm WASM @CMAKE_INSTALL_FULL_LIBDIR@ NO_DEFAULT_PATH)
find_library(libwast WAST @CMAKE_INSTALL_FULL_LIBDIR@ NO_DEFAULT_PATH)
find_library(libir IR     @CMAKE_INSTALL_FULL_LIBDIR@ NO_DEFAULT_PATH)
find_library(liblogging Logging @CMAKE_INSTALL_FULL_LIBDIR@ NO_DEFAULT_PATH)
find_library(libsoftfloat softfloat @CMAKE_INSTALL_FULL_LIBDIR@ NO_DEFAULT_PATH)
find_library(libbscrypto bscrypto @CMAKE_INSTALL_FULL_LIBDIR@ NO_DEFAULT_PATH)
find_library(libdecrepit decrepit @CMAKE_INSTALL_FULL_LIBDIR@ NO_DEFAULT_PATH)
find_library(libchainbase chainbase @CMAKE_INSTALL_FULL_LIBDIR@ NO_DEFAULT_PATH)
find_library(libbuiltins builtins @CMAKE_INSTALL_FULL_LIBDIR@ NO_DEFAULT_PATH)

#Ubuntu build requires rt library to be specified explicitly
if(UNIX AND NOT APPLE)
  find_library(LIBRT rt)
endif()

set(EOSIO_WASM_RUNTIMES @EOSIO_WASM_RUNTIMES@)
if("eos-vm-oc" IN_LIST EOSIO_WASM_RUNTIMES)
   set(WRAP_MAIN "-Wl,-wrap=main")
endif()

add_library(EosioChain INTERFACE)

target_link_libraries(EosioChain INTERFACE
   ${libchain}
   ${libfc}
   ${libwast}
   ${libwasm}
   ${libir}
   ${libsoftfloat}
   ${libbscrypto}
   ${libdecrepit}
   ${liblogging}
   ${libchainbase}
   ${libbuiltins}
   ${libsecp256k1}
   ${libbn256}
   ${libbls12-381}
   @GMP_LIBRARY@

   Boost::date_time
   Boost::filesystem
   Boost::system
   Boost::chrono
   Boost::multi_index
   Boost::multiprecision
   Boost::interprocess
   Boost::asio
   Boost::signals2
   Boost::iostreams
   "-lz" # Needed by Boost iostreams

   ${LLVM_LIBS}

   ${PLATFORM_SPECIFIC_LIBS}

   ${WRAP_MAIN}
   Threads::Threads
)

target_include_directories(EosioChain INTERFACE
                              @OPENSSL_INCLUDE_DIR@
                              @CMAKE_INSTALL_PREFIX@
                              @CMAKE_INSTALL_FULL_INCLUDEDIR@
                              @CMAKE_INSTALL_FULL_INCLUDEDIR@/wasm-jit
                              @CMAKE_INSTALL_FULL_INCLUDEDIR@/leapboringssl
                              @CMAKE_INSTALL_FULL_INCLUDEDIR@/softfloat )

#adds -ltr. Ubuntu eosio.contracts build breaks without this
if(UNIX AND NOT APPLE)
   target_link_libraries(EosioChain INTERFACE ${LIBRT})
endif()

add_library(EosioTester INTERFACE)

target_link_libraries(EosioTester INTERFACE
   ${libtester}
   Boost::unit_test_framework
   EosioChain
)

macro(add_eosio_test_executable test_name)
   add_executable( ${test_name} ${ARGN} )
   target_link_libraries( ${test_name}
       EosioTester
      )

endmacro()

macro(add_eosio_test test_name)
   add_eosio_test_executable( ${test_name} ${ARGN} )
   #This will generate a test with the default runtime
   add_test(NAME ${test_name} COMMAND ${test_name} --report_level=detailed --color_output)

   #Manually run unit_test for all supported runtimes
   #To run unit_test with all log from blockchain displayed, put --verbose after --, i.e. unit_test -- --verbose
endmacro()
